package com.xinsoft.controller.mes;

import com.xinsoft.common.Response;
import com.xinsoft.entity.po.MesFlowCardRuleRecord;
import com.xinsoft.entity.po.MesProcessPlan;
import com.xinsoft.service.MesFlowCardRuleRecordService;
import com.xinsoft.service.MesProcessPlanService;
import com.xinsoft.validator.ValidatorUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

/**
 * 生产流程卡规则记录
 *
 * @folder 智能制造/智造设置/生产流程卡规则记录
 */
@RestController
@Validated
@RequestMapping("/mes/flowCardRuleRecord")
public class MesFlowCardRuleRecordController {

    @Autowired
    private MesFlowCardRuleRecordService flowCardRuleRecordService;

    @Autowired
    private MesProcessPlanService mesProcessPlanService;

    /**
     * 新增规则记录
     */
    @PostMapping("/add")
    @Transactional(rollbackFor = Exception.class)
    public Response<?> add(@RequestBody MesFlowCardRuleRecord dto) {
        // 参数校验
        ValidatorUtils.validateEntity(dto);
        flowCardRuleRecordService.save(dto);
        if (dto.getRuleType().equals(10819)){
            MesProcessPlan mesProcessPlan = new MesProcessPlan();
            mesProcessPlan.setId(dto.getSourceId());
            mesProcessPlan.setFeedingNum(dto.getFeedingNum());
            mesProcessPlanService.updateById(mesProcessPlan);
        }
        return Response.succeed();
    }

    /**
     * 获取详情
     */
    @GetMapping("/detail")
    public Response<MesFlowCardRuleRecord> detail(@NotNull(message = "生产流程卡规则类型不能为空") @RequestParam(value = "ruleType") Integer ruleType,
                                                  @NotNull(message = "源单id不能为空") @RequestParam(value = "sourceId") Integer sourceId) {
        return Response.succeed(flowCardRuleRecordService.detail(ruleType,sourceId));
    }
}
